﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using KegCommunication;

namespace KegMonitor
{
    class TapReader
    {
        public byte position;
        public Timer endTrigger;
        public double value;
        public double initialValue;
        public DateTime time;
        public bool pourInProgress;
        private DateTime lastPourUpdate;
        private bool SentStartPour = false;

        public KegReader km;

        public TapReader(KegReader km)
        {
            this.km = km;
            endTrigger = new Timer();
            endTrigger.AutoReset = false;
            endTrigger.Elapsed += this.EndPour;
            endTrigger.Interval = Properties.Settings.Default.PourDelay;
        }

        public void Reset()
        {
            endTrigger.Stop();
            value = 0;
            initialValue = 0;
            pourInProgress = false;
            time = DateTime.Now;
            SentStartPour = false;
        }

        public void StartOrContinuePour(double start)
        {
            if (!pourInProgress)
            {
                // Store the initial value time and value from the pour
                initialValue = start;
                time = DateTime.Now;

                // Fire the start pour event
                lastPourUpdate = DateTime.Now;
                PourEventArgs pourEvent = new PourEventArgs(position, initialValue, time);
                km.OnStartPour(pourEvent);
            }
            else
            {
                lastPourUpdate = DateTime.Now;
                PourEventArgs pourEvent = new PourEventArgs(position, value - initialValue, time);
                km.OnContinuePour(pourEvent);
            }

            value = start;

            // Reset the timer for signaling the end of the pour.
            endTrigger.Stop();
            endTrigger.Interval = Properties.Settings.Default.PourDelay;
            endTrigger.Start();

            pourInProgress = true;
        }

        private void EndPour(object sender, ElapsedEventArgs e)
        {
            // The interval has elapsed since a pour was started
            endTrigger.Stop();

            // Check that the pour is larger than the minimum accepted value.
            // this prevents small movements in the line from triggering pours.
            double pourValue = value - initialValue;
            double pourMinimum = Properties.Settings.Default.PourMinimum;
            if (pourValue > pourMinimum)
            {
                // The pour was large enough to be recorded

                // Call the event handlers
                PourEventArgs pourEvent = new PourEventArgs(position, value - initialValue, time);
                km.OnEndPour(pourEvent);
            }
            else
            {
                KegLog.LogComment("A pour occured, but was not recorded due to low volume (most likely noise)");
            }

            // Reset all of the values
            Reset();
        }
    }
}
